package com.usns;

import android.content.Intent;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.*;
import weibo4j.Oauth;
import weibo4j.http.AccessToken;
import weibo4j.util.WeiboConfig;
import com.usns.user.*;

public class OauthActivity extends Activity {
	static final String redirect_uri = WeiboConfig.getValue("redirect_URI");
	Oauth auth = new Oauth();
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
        setContentView(R.layout.oauth);
        
        WebView browser = (WebView) findViewById(R.id.OauthView);
        
        browser.getSettings().setJavaScriptEnabled(true);
        browser.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url){
                if(!url.startsWith(redirect_uri)){
                	view.loadUrl(url);
                }else{
                	System.out.println("url:" + url);
                	String res = url.substring(redirect_uri.length() + 7);
                	System.out.println("res:" + res);
                	try{
                		//User user = User.getInstance();
                		//user.setSinaToken(token);
                		
                		Intent indent = new Intent(OauthActivity.this, FeedListActivity.class);
                		startActivity(indent);
                		finish();
                	}catch(Exception e){
                		//not handled
                	}
                }
                return true;
           }
        });
        browser.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress){
            	setProgress(progress * 100);
            }
        });
        
        try{
        	String auth_url = auth.authorize("token");
        	System.out.println(auth_url);
        	browser.loadUrl(auth_url);
        }catch(Exception e){
        	
        }
	}
}
